import 'package:flutter/material.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';
import 'package:flutter_markdown/flutter_markdown.dart';
import 'event_inquiry_page.dart';

class SeptemberPage extends StatelessWidget {
  const SeptemberPage({super.key});

  @override
  Widget build(BuildContext context) {
    return _AutumnCollaborationView();
  }
}

class _AutumnCollaborationView extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return Container(
      decoration: const BoxDecoration(
        image: DecorationImage(
          image: AssetImage('assets/images/my-bg.png'),
          fit: BoxFit.cover,
        ),
      ),
      child: Scaffold(
        backgroundColor: Colors.transparent,
        body: SafeArea(
          bottom: false,
          child: Column(
            children: [
              _navigationHeader(context),
              Expanded(
                child: SingleChildScrollView(
                  padding: EdgeInsets.symmetric(horizontal: 0.w),
                  child: _paperStackLayout(),
                ),
              ),
              _createRegistrationButton(context),
            ],
          ),
        ),
      ),
    );
  }

  Widget _navigationHeader(BuildContext context) {
    return Container(
      padding: EdgeInsets.symmetric(horizontal: 16.w, vertical: 10.h),
      child: Row(
        children: [
          GestureDetector(
            onTap: () => Navigator.of(context).pop(),
            child: Icon(
              Icons.arrow_back_ios_new,
              color: Colors.white,
              size: 20.w,
            ),
          ),
          Expanded(
            child: Center(
              child: Text(
                'Competition Rules',
                style: TextStyle(
                  fontSize: 22.sp,
                  fontWeight: FontWeight.w600,
                  color: Colors.white,
                  fontFamily: 'Poppins',
                ),
              ),
            ),
          ),
          SizedBox(width: 26.w),
        ],
      ),
    );
  }

  Widget _createRegistrationButton(BuildContext context) {
    return Container(
      padding: EdgeInsets.symmetric(horizontal: 20.w, vertical: 20.h),
      child: SizedBox(
        width: double.infinity,
        height: 50.h,
        child: ElevatedButton(
          onPressed: () {
            Navigator.of(context).push(
              MaterialPageRoute(
                builder:
                    (context) => const EventInquiryPage(eventType: 'september'),
              ),
            );
          },
          style: ElevatedButton.styleFrom(
            backgroundColor: const Color(0xFFCD8B76),
            shape: RoundedRectangleBorder(
              borderRadius: BorderRadius.circular(25.r),
            ),
            elevation: 0,
          ),
          child: Text(
            'Sign Up',
            style: TextStyle(
              fontSize: 18.sp,
              fontWeight: FontWeight.w600,
              color: Colors.white,
              fontFamily: 'Poppins',
            ),
          ),
        ),
      ),
    );
  }

  Widget _buildActionMenuButton() {
    return FloatingActionButton(
      onPressed: () {
        // 展开菜单功能实现
      },
      backgroundColor: const Color(0xFFCD8B76),
      child: Icon(Icons.menu, color: Colors.white, size: 24.w),
    );
  }

  Widget _paperStackLayout() {
    final List<Widget> backgroundPapers = _generateBackgroundPapers();

    return Stack(
      children: [
        ...backgroundPapers,
        Column(
          children: [_collaborationContentRenderer(), SizedBox(height: 40.h)],
        ),
      ],
    );
  }

  List<Widget> _generateBackgroundPapers() {
    final paperPositions = [0.0, 620.0, 1200.0, 1600.0, 2000.0];

    return paperPositions.map((topPosition) {
      return Positioned(
        top: topPosition.h,
        left: 0.w,
        right: 0.w,
        child: Container(
          width: 446.w,
          height: 667.h,
          decoration: const BoxDecoration(
            image: DecorationImage(
              image: AssetImage('assets/images/paper-bg.png'),
              fit: BoxFit.contain,
            ),
          ),
        ),
      );
    }).toList();
  }

  Widget _collaborationContentRenderer() {
    return _TeamCompetitionContent();
  }
}

class _TeamCompetitionContent extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return Padding(
      padding: EdgeInsets.all(24.w),
      child: Column(
        crossAxisAlignment: CrossAxisAlignment.start,
        mainAxisSize: MainAxisSize.min,
        children: [
          SizedBox(height: 20.h),
          _buildCompetitionHeader(),
          SizedBox(height: 20.h),
          _buildFeatureImage(),
          SizedBox(height: 20.h),
          _renderCompetitionRules(),
        ],
      ),
    );
  }

  Widget _buildCompetitionHeader() {
    return Text(
      'Offline Collaboration Performance Competition Rules',
      style: TextStyle(
        color: Colors.white,
        fontSize: 15.sp,
        fontFamily: 'Porter Sans Block',
        fontWeight: FontWeight.w400,
      ),
    );
  }

  Widget _buildFeatureImage() {
    return Container(
      width: double.infinity,
      height: 168.h,
      decoration: BoxDecoration(
        image: const DecorationImage(
          image: AssetImage('assets/images/september-image.png'),
          fit: BoxFit.cover,
        ),
      ),
    );
  }

  Widget _renderCompetitionRules() {
    return Markdown(
      data: _getTeamCollaborationRules(),
      styleSheet: _createMarkdownTheme(),
      physics: const NeverScrollableScrollPhysics(),
      shrinkWrap: true,
    );
  }

  MarkdownStyleSheet _createMarkdownTheme() {
    return MarkdownStyleSheet(
      p: TextStyle(
        color: Colors.white,
        fontSize: 12.sp,
        fontFamily: 'Poppins',
        fontWeight: FontWeight.w400,
      ),
      h1: TextStyle(
        color: Colors.white,
        fontSize: 15.sp,
        fontFamily: 'Poppins',
        fontWeight: FontWeight.w700,
      ),
      h2: TextStyle(
        color: Colors.white,
        fontSize: 12.sp,
        fontFamily: 'Poppins',
        fontWeight: FontWeight.w400,
      ),
    );
  }

  String _getTeamCollaborationRules() {
    return '''
# 1. Basic Rules

The goal of this competition is for teams to collaborate and perform a designated piece or an original composition, showcasing teamwork and musical skills. The best-performing team will be awarded based on their accuracy, creativity, and harmony.

• Each team must consist of 2-5 members, using any combination of instruments (e.g. guitar, ukulele, piano, violin, drum, etc.).

• Teams must register through the competition page in the App by submitting team information and their preferred performance piece.

• Teams can also create profiles (e.g. instrument type, music style) to introduce themselves to spectators and judges.

• **Pre-assigned Song**: Choose a song from the competition's provided list.

• **Original Composition**: Submit an original song with its complete sheet music and rehearsal recording for pre-approval.

# 2. Venue and Equipment Rules

## 1. Provided Equipment:
• The organizer will supply common instruments such as pianos, electronic drums, and amplifiers for electric guitars or basses.
• Team members must bring their personal portable instruments (e.g. guitars, ukuleles, violins).

## 2. Rehearsal Time:
• Each team will be given 15 minutes rehearsal time to test sound and prepare for their performance.

## 3. App Function Integration:
• Teams can use the App to access the schedule, set reminders, and track their performance results.

# 3. Rewards

## 1. Best Team Performance Award:
• Instruments or accessories (e.g. guitars, ukuleles, strings) provided by sponsors.

## 2. Creativity Award:
• Gift cards, premium subscriptions in the App, or professional music lessons.

## 3. Audience Choice Award:
• Voted by the audience, this award comes with medals or virtual badges for the winning team.
''';
  }
}
